/**
 * Class ImageCycle
 */
ninejs.extend(ninejs, function( exports ) {
	function ImageCycle( options ) {
		if ( !options || !options.render ) return;
		
		if ( !(this instanceof ImageCycle) ) {
			return new ImageCycle(options);	
		} else {
			if ( !options.speed ) options.speed = 5;
			if ( !options.timeout ) options.timeout = 1000;
			if ( !options.interval ) options.interval = 20;
			if ( !options.unit ) options.unit = 150;
			if ( typeof(options.openAuto) === "undefined" ) options.openAuto = false;
			
			exports.extend(this, options);
			
			this.init();	
		}
	}

	ImageCycle.prototype = {
		intervalId: null,
		timeoutId: null,
		direction: 1,
		isStop: true,
		init: function () {
			var _this = this,
				el = exports.getElemById(this.render),
				liList = [],
				parentEl, prevEl, nextEl, childEl, i, len, childrenEl;
			
			if ( !el ) {
				return;
			} else {
				childEl = el.children[0];
			}

			if ( !childEl || !childEl.children ) return;
			if ( childEl.children.length <= 1 ) return;

			if ( el && childEl ) {
				i = 0;
				len = childEl.children.length;				

				for ( ; i < len; i++ ) {
					childrenEl = childEl.children[i];
					if ( childrenEl.nodeType == 1 && childrenEl.nodeName.toLowerCase() == "li" ) {
						liList.push(childrenEl);
					}
				}
				
				this.elem = childEl;
				this.length = liList.length;
				this.width = this.unit * this.length;
				this.elem.style.width = this.width + "px";				
				this.elem.style.marginLeft = "0px";
			}

			if ( el && (parentEl = el.parentNode) ) {
				this.parentEl = parentEl;
				
				if ( parentEl.children.length > 1 ) {
					if ( prevEl = parentEl.children[0].children[0] ) {
						this.prevEl = prevEl;
						exports.EventEntry.addEvent(this.prevEl, "click", function( e ) {
							_this.start(e);								   
						});
						exports.EventEntry.addEvent(this.prevEl, "mouseout", function( e ) {
							var mleft = parseInt(_this.elem.style.marginLeft);
							if ( mleft <= (_this.unit - _this.width) ) {
								_this.openAuto && _this.autoRun();
							}
						});
					}
					if ( nextEl = parentEl.children[parentEl.children.length - 1].children[0] ) {
						this.nextEl = nextEl;
						exports.EventEntry.addEvent(this.nextEl, "click", function( e ) {
							_this.start(e);  
						});
						exports.EventEntry.addEvent(this.nextEl, "mouseout", function( e ) {
							var mleft = parseInt(_this.elem.style.marginLeft);
							if ( mleft >= 0 ) {
								_this.openAuto && _this.autoRun();
							}
						});
					}
				}
				
				exports.EventEntry.addEvent(this.elem, "mouseover", function( e ) {
					_this.stop();  
				});
				exports.EventEntry.addEvent(this.elem, "mouseout", function( e ) {
					_this.openAuto && _this.ready();
				});				
			}
			
			if ( this.openAuto ) {
				this.direction = -1;
				this.run();
			}
		},
		ready: function () {
			var mleft = parseInt(this.elem.style.marginLeft);
			
			if ( mleft >= 0 || mleft <= (this.unit - this.width) ) {
				this.direction = -this.direction;
			}
			
			this.isStop = true;
			this.run();			
		},
		start: function ( e ) {
			var evt = e || window.event,
				target = evt.target || evt.srcElement;																	   

			if ( target.getAttribute("data-id") == "prev" ) {
				this.direction = -1;				
			} else {
				this.direction = 1;
			}
				
			clearTimeout(this.timeoutId);
			this.isStop = true;
			this.run();
		},
		stop: function () {
			var mleft = parseInt(this.elem.style.marginLeft);
			
			if ( mleft % this.unit == 0 ) {
				this.clear();
			}
		},
		clear: function () {
			clearTimeout(this.timeoutId);
			clearInterval(this.intervalId);
			this.isStop = true;
		},
		autoRun: function () {
			var _this = this;
			
			clearTimeout(this.timeoutId);
			this.timeoutId = setTimeout(function(){
				_this.ready();
			}, this.timeout);
		},
		run: function () {
			var _this = this,
				mleft = parseInt(this.elem.style.marginLeft);
			
			if ( this.direction == -1 && mleft <= (this.unit - this.width) ) return;
			if ( this.direction == 1 && mleft >= 0 ) return;
			
			if ( this.isStop ) {
				clearInterval(this.intervalId);
				this.intervalId = setInterval(function(){
					_this.move();								   
				}, this.interval);
				
				this.elem.style.marginLeft = mleft + this.direction * this.speed + "px";
			}
		},
		move: function () {
			var mleft = parseInt(this.elem.style.marginLeft);
			
			if ( mleft % this.unit == 0 ) {
				this.clear();
				this.openAuto && this.autoRun();
			} else {
				mleft = mleft + this.direction * this.speed;
				
				this.elem.style.marginLeft = mleft + "px";
				this.isStop = false;
			}
		}
	};

	exports.ImageCycle = ImageCycle;
});